package com.android.youwenchuang;

import android.app.Application;
import android.content.Context;
import android.content.res.AssetManager;

import com.android.youwenchuang.bean.Goods;
import com.android.youwenchuang.bean.News;
import com.android.youwenchuang.bean.User;
import com.android.youwenchuang.gen.DaoMaster;
import com.android.youwenchuang.gen.DaoSession;
import com.android.youwenchuang.util.TitleBarStyle;
import com.blankj.utilcode.util.GsonUtils;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.TitleBar;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

public class App extends Application {

    private DaoSession daoSession;

    public static App mApp;

    private AssetManager assetManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
        TitleBar.setDefaultStyle(new TitleBarStyle());
        // regular SQLite database
        ExampleOpenHelper helper = new ExampleOpenHelper(this, "my_db");
        Database db = helper.getWritableDb();
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
        daoSession = new DaoMaster(db).newSession();
        initData();
    }

    private void initData() {
        if (daoSession.getGoodsDao().count() == 0) {
            assetManager = getAssets();
            String goods = getData("goods.json");
            List<Goods> goodsList = GsonUtils.fromJson(goods, new TypeToken<List<Goods>>(){}.getType());
            for (int i = 0; i < goodsList.size(); i++) {
                goodsList.get(i).setDate(new Date());
            }
            daoSession.getGoodsDao().insertInTx(
                    goodsList.toArray(new Goods[0])
            );

            // 新闻数据
            String news = getData("news.json");
            List<News> newsList = GsonUtils.fromJson(news, new TypeToken<List<News>>(){}.getType());
            for (int i = 0; i < newsList.size(); i++) {
                newsList.get(i).setDate(new Date());
            }
            daoSession.getNewsDao().insertInTx(
                    newsList.toArray(new News[0])
            );
            User user1 = new User();
            user1.setName("小王");
            User user2 = new User();
            user2.setName("小三");
            User user3 = new User();
            user3.setName("小李子");
            User user4 = new User();
            user4.setName("张大叔");
            App.mApp.getDaoSession().getUserDao().insertInTx(user1, user2, user3, user4);
        }
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public static class ExampleOpenHelper extends DaoMaster.OpenHelper {

        public ExampleOpenHelper(Context context, String name) {
            super(context, name);
        }

        @Override
        public void onCreate(Database db) {
            super.onCreate(db);
        }
    }

    private String getData(String fileName) {
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 现在 stringBuilder 包含文件内容
        return stringBuilder.toString();
    }

}
